package com.pope.framework.third;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.pope.framework.config.PatchConfig;
import com.pope.framework.core.util.StringUtil;
import com.pope.framework.kernel.model.exception.ServiceException;
import com.pope.framework.util.TokenContext;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
@Component
@Slf4j
public class ThirdUtil {
	@Autowired
	private PatchConfig pathConfig;
	private final static String gurl="http://www.scoway.cn:8020/service";
	
	public  String getDepartName(List<Map<String,Object>> departs,Integer value) {
		if(CollUtil.isNotEmpty(departs) && value!=null) {
			for(Map<String,Object> depart:departs) {
				int departId=StringUtil.toInt(depart.get("id"));
				if(value==departId) {
					return StringUtil.toStr(depart.get("name"));
				}
			}
		}
		return "";
	}
	
	private String getDepartUrl() {
		String url=pathConfig.getPath();
		if(StrUtil.isBlank(url)) {
			return null;
		}
		if(!url.trim().endsWith("/")) {
			url+="/";
		}
		url+=pathConfig.getDepartUrl();
		if(!url.trim().endsWith("/")) {
			url+="/";
		}
		return url;
	}
	public  List<Map<String,Object>> getDeparts(Integer hosId){
		try {
			//String url=getDepartUrl()+"dept/tree?site=1&token="+StringUtil.toStr(TokenContext.getToken())+"&appid=18&stype=2,5&filter=0,"+hosId;
			String url=getDepartUrl()+"dept/list?site="+StringUtil.toStr(TokenContext.getSite())+"&token="+StringUtil.toStr(TokenContext.getToken())+"&appid="+StringUtil.toStr(TokenContext.getApp())+"&stype=5&filter="+hosId;
			String value=HttpUtil.get(url);
			if(StrUtil.isNotBlank(value)) {
				Map<String,Object> data=JSONUtil.toBean(value, Map.class);
				if("0".equals(StringUtil.toStr(data.get("ret")))) {
					if(data.get("data")!=JSONNull.NULL) {
						List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
						return list;
					}
					
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			throw new ServiceException("科室列表获取异常！");
		}
		return null;
	}
	
	public  List<Map<String,Object>> getDepartsPeople(Integer hosId){
		try {
			String url=getDepartUrl()+"dept/list?site="+StringUtil.toStr(TokenContext.getSite())+"&token="+StringUtil.toStr(TokenContext.getToken())+"&appid="+StringUtil.toStr(TokenContext.getApp())+"&stype=5&filter="+hosId;
			String value=HttpUtil.get(url);
			if(StrUtil.isNotBlank(value)) {
				Map<String,Object> data=JSONUtil.toBean(value, Map.class);
				if("0".equals(StringUtil.toStr(data.get("ret")))) {
					if(data.get("data")!=JSONNull.NULL) {
						List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
						return list;
					}
					
				}
			}
		}catch(Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			throw new ServiceException("科室列表获取异常！");
		}
		return null;
	}
	
	public String getDepartNameById(Integer departId) {
		Map<String,Object> map=this.getDepartById(departId);
		if(CollUtil.isNotEmpty(map)) {
			return StringUtil.toStr(map.get("name"));
		}
		
		return "";
	}
	public  Map<String,Object> getDepartById(Integer departId){
		//String ur=gurl+"/114/dept/"+departId+"?site=1&token="+TokenContext.get()+"&appid=18&stype=2&filter=0";
		try {
		String url=this.getDepartUrl()+"dept/"+departId+"?site="+StringUtil.toStr(TokenContext.getSite())+"&token="+StringUtil.toStr(TokenContext.getToken())+"&appid="+StringUtil.toStr(TokenContext.getApp())+"&stype=2&filter=0";
		String value=HttpUtil.get(url);
		if(StrUtil.isNotBlank(value)) {
			Map<String,Object> data=JSONUtil.toBean(value, Map.class);
			if("0".equals(StringUtil.toStr(data.get("ret")))) {
				if(data.get("data")!=JSONNull.NULL) {
					List<Map<String,Object>> list=(List<Map<String,Object>>)data.get("data");
					if(CollUtil.isNotEmpty(list)) {
						return list.get(0);
					}
				}
				
			}
		}
		}catch(Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			throw new ServiceException("根据科室ID获取科室信息异常");
		}
		return null;
	}
	
	public  boolean voliadeToken(String token) {
		String url=pathConfig.getPath();
		if(StrUtil.isBlank(url)) {
			return false;
		}
		if(!url.trim().endsWith("/")) {
			url+="/";
		}
		url+=pathConfig.getTokenUrl();
		if(!url.trim().endsWith("/")) {
			url+="/";
		}
		url=url+"admin/token/check?appid="+StringUtil.toStr(TokenContext.getApp())+"&site="+StringUtil.toStr(TokenContext.getSite())+"&token="+token;
		Map<String,Object> map=CollUtil.newHashMap();
		try {
		String value=HttpUtil.post(url, map);
		if(StrUtil.isNotBlank(value)) {
			Map<String,Object> data=JSONUtil.toBean(value, Map.class);
			if("0".equals(StringUtil.toStr(data.get("Ret")))) {
				if(StringUtil.toInt(data.get("Userid"))>0) {
					return true;
				}
			}
		}
		}catch(Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			throw new ServiceException("TOKEN检测信息异常");
		}
		return false;
	}
}
